<template>
  <div>
    <div class="table-header">
      <h4>物料明细清单</h4>
      <el-button v-if="showAdd && editable" type="primary" size="mini" @click="addRow">添加物料</el-button>
    </div>
    
    <div class="table-content">
      <el-table :data="value" border stripe size="mini" style="width: 100%">
        <el-table-column prop="partNumber" label="料号" width="140" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.partNumber" size="mini" placeholder="料号" />
            </div>
            <span v-else class="cell-text">{{ scope.row.partNumber }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="materialName" label="物料名称" width="180" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.materialName" size="mini" placeholder="物料名称" />
            </div>
            <span v-else class="cell-text">{{ scope.row.materialName }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="materialSpec" label="规格" width="160" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.materialSpec" size="mini" placeholder="规格" />
            </div>
            <span v-else class="cell-text">{{ scope.row.materialSpec }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="unit" label="单位" width="100" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.unit" size="mini" placeholder="单位" />
            </div>
            <span v-else class="cell-text">{{ scope.row.unit }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="requiredQuantity" label="需求数量" width="120" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input-number 
                v-model="scope.row.requiredQuantity" 
                size="mini" 
                :min="0" 
                :precision="0"
                placeholder="需求数量"
                style="width: 100%"
              />
            </div>
            <span v-else class="cell-text">{{ scope.row.requiredQuantity }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="issuedQuantity" label="已发数量" width="120" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input-number 
                v-model="scope.row.issuedQuantity" 
                size="mini" 
                :min="0" 
                :precision="0"
                placeholder="已发数量"
                style="width: 100%"
              />
            </div>
            <span v-else class="cell-text">{{ scope.row.issuedQuantity }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="remainingQuantity" label="未发数量" width="120" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input-number 
                v-model="scope.row.remainingQuantity" 
                size="mini" 
                :min="0" 
                :precision="0"
                placeholder="未发数量"
                style="width: 100%"
              />
            </div>
            <span v-else class="cell-text">{{ scope.row.remainingQuantity }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="materialCategory" label="分类" width="140" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.materialCategory" size="mini" placeholder="分类" />
            </div>
            <span v-else class="cell-text">{{ scope.row.materialCategory }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="bomIdentifier" label="BOM标识" width="140" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.bomIdentifier" size="mini" placeholder="BOM标识" />
            </div>
            <span v-else class="cell-text">{{ scope.row.bomIdentifier }}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="remarks" label="备注" width="200" align="center">
          <template slot-scope="scope">
            <div v-if="editable">
              <el-input v-model="scope.row.remarks" size="mini" placeholder="备注" />
            </div>
            <span v-else class="cell-text">{{ scope.row.remarks }}</span>
          </template>
        </el-table-column>
        
        <el-table-column v-if="showDelete && editable" label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button 
              type="danger" 
              size="mini" 
              icon="el-icon-delete"
              @click="removeRow(scope.$index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <div class="table-footer" v-if="value.length === 0">
      <el-empty description="暂无物料明细数据"></el-empty>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ProductionDetailTable',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    showAdd: {
      type: Boolean,
      default: true
    },
    showDelete: {
      type: Boolean,
      default: true
    },
    editable: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    // 添加新行
    addRow() {
      const newRow = {
        id: null,
        workOrderId: null,
        partNumber: '',
        materialName: '',
        materialSpec: '',
        materialCategory: '',
        productName: '',
        drawingNumber: '',
        unit: '',
        surfaceTreatment: '',
        requiredQuantity: 0,
        issuedQuantity: 0,
        remainingQuantity: 0,
        bomIdentifier: '',
        materialBrand: '',
        remarks: '',
        createdAt: null,
        updatedAt: null
      }
      this.$emit('input', [...this.value, newRow])
    },
    
    // 删除行
    removeRow(index) {
      const newData = [...this.value]
      newData.splice(index, 1)
      this.$emit('input', newData)
    }
  }
}
</script>

<style scoped>
.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px 0;
  border-bottom: 1px solid #ebeef5;
}

.table-header h4 {
  margin: 0;
  color: #2980b9;
  font-size: 16px;
}

.table-content {
  width: 100%;
}

.table-footer {
  text-align: center;
  padding: 20px;
}

.el-table {
  margin-bottom: 15px;
}

.el-input-number {
  width: 100%;
}

.cell-text {
  display: inline-block;
  padding: 0 6px;
}
</style> 